package com.lsh.airticket.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lsh.airticket.dto.Result;
import com.lsh.airticket.entity.Model;
import com.lsh.airticket.exception.UserException;
import com.lsh.airticket.service.ModelService;
import com.lsh.airticket.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lsh
 * @date 2021/2/17
 **/
@RestController
@RequestMapping("/flights")
public class AdminModelController {

    @Autowired
    ModelService modelService;

    @GetMapping("/admin/model/findMoleList")
    public Result findModelList(Integer page) {
        if (page == null || page == 0) page = 1;
        PageHelper.startPage(page, Constants.PAGE_SIZE);
        PageInfo<Model> pageInfo = modelService.findModelList();
        return Result.result().addData("pageInfo", pageInfo);
    }

    @GetMapping("/admin/model/findMoleAll")
    public Result findModelAll() {
        List<Model> modelAll = modelService.findModelAll();
        return Result.result().addData("modelAll", modelAll);
    }

    /**
     * 插入 一条 飞机 数据
     * @param model
     * @return
     */
    @PostMapping("/admin/model/insertModel")
    public Result insertModel(@RequestBody Model model) {
        int i = modelService.insertTbModel(model);
        if (i != 1) {
            throw new UserException("插入失败");
        }
        return Result.result().setMsg("插入成功");
    }

    /**
     * 修改一条数据
     * @param model
     * @return
     */
    @PostMapping("/admin/model/updateModel")
    public Result update(@RequestBody Model model) {
        int update = modelService.update(model);
        if (update != 1) {
            throw new UserException("修改失败");
        }
        return Result.result().setMsg("修改成功");
    }

    /**
     * 根据主键删除一条数据
     */
    @GetMapping("/admin/model/deleteModel")
    public Result deleteModel(Integer modelId) {
        int delete = modelService.delete(modelId);
        if (delete != 1) {
            throw new UserException("删除失败");
        }
        return Result.result().setMsg("删除成功");
    }

}
